//
//  SafeMainView.m
//  AppleMarket
//
//  Created by yangy on 08/11/2018.
//  Copyright © 2018 szgsy. All rights reserved.
//

#import "SafeMainView.h"
// view
#import "BH_UnderLineTextField.h"
// viewModel
#import "SafePwdViewModel.h"
#import "ImageCodeMoel.h"

@interface SafeMainView()
@property (nonatomic, strong) UIView *blockView;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UIButton *closeButton;
@property (nonatomic, strong) UIView *lineView;
@property (nonatomic, strong) BH_UnderLineTextField *codeTF;
@property (nonatomic, strong) UIButton *completeButton;
@property (nonatomic, strong) UIButton *forgetButton;
@property (nonatomic, strong) SafePwdViewModel *viewModel;
@end
@implementation SafeMainView

- (instancetype)initWithFrame:(CGRect)frame viewModel:(id) viewModel
{
    self = [super initWithFrame:frame];
    if(self){
        _viewModel = viewModel;
        self.backgroundColor = [UIColor clearColor];
        [self initUI];
        [self bindViewModel];
    }
    return self;
}

- (void) bindViewModel
{
    RAC(_viewModel,pwdStr_) = _codeTF.rac_textSignal;
    RAC(_completeButton,enabled) = [RACSignal combineLatest:@[RACObserve(_codeTF, text)] reduce:^id (NSString *codeString){
        return @(codeString.length > 0);
    }];
    RAC(_completeButton,backgroundColor) = [RACSignal combineLatest:@[RACObserve(_codeTF, text)] reduce:^id (NSString *codeString){
        return codeString.length > 0? kColorWithHex(0x3291EB) : kColorWithHex(0xADB7C1);
    }];
}

- (void) initUI
{
    [self addSubview:self.blockView];
    [self.blockView addSubview:self.codeTF];
    [self.blockView addSubview:self.forgetButton];
    [self.blockView addSubview:self.titleLabel];
    [self.blockView addSubview:self.closeButton];
    [self.blockView addSubview:self.lineView];
    [self.blockView addSubview:self.completeButton];
    [self setViewsLayout];
}

- (void) setViewsLayout
{
    [_blockView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self);
        make.right.mas_equalTo(self);
        make.height.mas_equalTo(270);
        make.bottom.mas_equalTo(self);
    }];
    [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.blockView).offset(15);
        make.top.mas_equalTo(self.blockView).offset(20);
    }];
    [_closeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self.blockView).offset(-15);
        make.centerY.mas_equalTo(self.titleLabel.mas_centerY);
    }];
    
    [_lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.titleLabel.mas_bottom).offset(14);
        make.left.mas_equalTo(self.blockView);
        make.right.mas_equalTo(self.blockView);
        make.height.mas_equalTo(1);
    }];
    
    [_codeTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.lineView.mas_bottom).offset(40);
        make.left.mas_equalTo(self.titleLabel);
        make.right.mas_equalTo(self.closeButton);
    }];
    
    [_forgetButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self.codeTF.mas_right);
        make.top.mas_equalTo(self.codeTF.mas_bottom).offset(10);
    }];
    
    [_completeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(self.forgetButton.mas_bottom).offset(45);
        make.left.mas_equalTo(self.codeTF);
        make.right.mas_equalTo(self.codeTF);
        make.height.mas_equalTo(40);
    }];
}

- (UIView *)blockView
{
    if(!_blockView){
        _blockView = [[UIView alloc] init];
        _blockView.backgroundColor = [UIColor whiteColor];
    }
    return _blockView;
}

- (UILabel *)titleLabel
{
    if(!_titleLabel){
        _titleLabel = [UILabel cm_labelWithText:@"请输入资金密码" font:kFontBold(16) textColor:kColorWithHex(0x0E304C)];
    }
    return _titleLabel;
}

- (UIButton *)closeButton
{
    if(!_closeButton){
        _closeButton = [UIButton cm_ButtonWithTitle:@"取消"  font:kFont(13) titleColor:kColorWithHex(0xADB7C1)];
        @weakify(self);
        [[_closeButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
            @strongify(self);
            [self.viewModel.closeSubject sendNext:nil];
        }];
    }
    return _closeButton;
}

- (UIView *)lineView
{
    if(!_lineView){
        _lineView = [[UIView alloc] init];
        _lineView.backgroundColor = kColorWithHex(0xCFD6DB);
    }
    return _lineView;
}

- (BH_UnderLineTextField *)codeTF
{
    if(!_codeTF){
        _codeTF = [[BH_UnderLineTextField alloc] initWithFrame:CGRectMake(30, 0, k_ScreenWidth - 60, 44)];
        _codeTF.placeholder = @"请输入资金密码";
        _codeTF.font = kFont(13);
        _codeTF.autocapitalizationType = UITextAutocapitalizationTypeNone;
        _codeTF.secureTextEntry = YES;
    }
    return _codeTF;
}

- (UIButton *)forgetButton
{
    if(!_forgetButton){
        _forgetButton = [UIButton cm_ButtonWithTitle:@"忘记密码?" font:kFont(12) titleColor:kColorWithHex(0x3291EB)];
        _forgetButton.titleLabel.textAlignment = NSTextAlignmentRight;
        _forgetButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
        @weakify(self);
        [[_forgetButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
            @strongify(self);
            [self.viewModel.forgetSubject sendNext:nil];
        }];
    }
    return _forgetButton;
}

- (UIButton *)completeButton
{
    if(!_completeButton){
        _completeButton = [UIButton cm_ButtonWithTitle:@"确定" font:kFont(14) titleColor:[UIColor whiteColor] bgColor:kColorWithHex(0x3291EB)];
        @weakify(self);
        [[_completeButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
            @strongify(self);
            if(self.codeTF.text.length < 6 || self.codeTF.text.length > 20){
                self.codeTF.error = @"资金密码必须6~20位，包括数字、字母、特殊字符。";
                return;
            }
            [self.viewModel.completeSubject sendNext:nil];
        }];
    }
    return _completeButton;
}


@end
